<?php


class BlogController extends Zend_Controller_Action {

	/**
	 *
	 * @todo Blogeinträge mit Suche und Selektion nach Blog-Einträgen
	 */
	public function indexAction() {
		return $this->_helper->redirector('show');
	}

	/**
	 * browse eines Blog-Eintrages
	 * Key id (blogid) aus url
	 */
	public function showAction() {

		// $form = new Application_Form_Blog();
		$blogid = (int) $this->_getParam('id');

		$blog_map = new Application_Model_BlogMapper();
		$blog = $blog_map->find($blogid);

		$created = $blog->getCreated();
		$userId = $blog->getUserId();
		$title = $blog->getTitle();
		$posting = $blog->getPosting();
		$username = $blog->getUser()->getRealName();

		$this->view->blogid = $blogid;
		$this->view->created = $created;
		$this->view->username = $username;
		$this->view->userId = $userId;
		$this->view->userLogin = $this->_helper->isloggedin();
		$this->view->title = $title;
		$this->view->posting = $posting;

		/** @var Zend_Navigation_Container $container */
		/* navigation erweitern
		$container = $this->view->navigation();
		$container->addPage(Zend_Navigation_Page::factory(array('uri' => 'http://www.example.com/', 'label' => "jo")));
		$page3 = $container->findOneByLabel('URLs');
		$container->removePage($page3);
		*/
	}

	/**
	 * Editieren Blog-Eintrag (in Formular)
	 * Key id (blogid) aus url
	 *
	 * Bei Neueintrag wird Login-User angenommen
	 * Bei Mutation wird geprüft, ob login-User gleich blogpost-userId
     *
	 * @throws Exception
	 */
	public function editAction() {
		//URL Parms + session
		$blogId = (int) $this->_getParam('id');
		$userLogin  = $this->_helper->isloggedin();
		if ($userLogin!=null) {
		 $userId = $userLogin->getId();
	    }

		$flashMessenger = $this->_helper->getHelper('FlashMessenger');
		$form = new Application_Form_Edit();

		$request = $this->getRequest();
		if ($this->getRequest()->isPost()) {

			if ($form->isValid($request->getPost())) {

				$blogEntry = new Application_Model_Blog($form->getValues());
				$mapper = new Application_Model_BlogMapper();

				if ($blogId != 0) { /* vorhandener Rec-> User Prüfen */
					// User holen und kontrollieren
					$blogEntry01 = $mapper->find($blogId);
					$blogId = $blogEntry->getId();
				}

				// Plausi Berechtigung od. falsche Aufrufe
				if ($blogId == "" && $userId == null) {
					throw new Exception('Applikationsfehler - Neuerfassung (nicht eingeloggt)');
				}
				if ($blogId != "" && ($blogEntry01->getUserid()) != $userId) {
					throw new Exception('Applikationsfehler - Mutation ohne Berechtigung');
				}

				if ($this->getRequest()->getParam("delete")) {
					$mapper->deleteRow($blogEntry->getId());
					$flashMessenger->addMessage('Record deleted');
					return $this->_helper->redirector->gotoUrl("user/show/userId/$userId");
				} else {
					// save und new zusammengefasst
					// bei "new" muss primary key null sein
					if ($blogEntry->getId() == 0) {
						$blogEntry->setId(null);
						$flashMessenger->addMessage('Record added');
					}
					else {
						$flashMessenger->addMessage('Record mutiert');
					}
					$blogEntry->setUserid($userId); // immer login-User
					$mapper->save($blogEntry);

					return $this->_helper->redirector->gotoUrl("user/show/userId/$userId");
				}

			}
		}

		// Form aufbereiten
		$blogNew = new Application_Model_Blog();
		$blog_map = new Application_Model_BlogMapper();

		$blogid = (int) $this->_getParam('id');
		if ($blogid == 0) { /*neuer Eintrag  */
			$viewUser = $userId; //eingeloggter User
			$blogNew->setUserid($viewUser);
			$created = new datetime();
			$action = 'new';
			$username = $userLogin->realName;
		}

		else { /* modify        */
			$blog = $blog_map->find($blogid);

			$title = $blog->getTitle();
			$posting = $blog->getPosting();
			$viewUser = $blog->getUserid();
			$created = $blog->getCreated();
			$username = $blog->getUser()->getRealName();

			$form->getElement('title')->setValue($title);
			$form->getElement('posting')->setValue($posting);
			$form->getElement('id')->setValue($blogid);
			$action = 'mod';

		}
        $this->view->action = $action;
		$this->view->form = $form;
		$this->view->userId = $viewUser;
		$this->view->userLogin = $userLogin;
		$this->view->username =  $username;
		$this->view->created = $created;
		$this->view->blogId = $blogid;
	}

}
